﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmLeaveRecord : Form
    {
        LeaveRecordInfo _LeaveRecordInfo;
        int _EmployeeID;
        int _LeaveRecordID;
        bool isChange = false;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        LeaveRecordClient wcfLeaveRecord;
        public frmLeaveRecord(int LeaveRecordID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            Language();
            dlgResult = DialogResult.Cancel;
            listRole = _listRole;
            _LeaveRecordInfo = new LeaveRecordInfo();
            wcfLeaveRecord = Service.LeaveRecord();
            _LeaveRecordInfo.ID = LeaveRecordID;
            _EmployeeID = EmployeeID;
            _LeaveRecordID = LeaveRecordID;
            if (LeaveRecordID > 0)
            {
                _LeaveRecordInfo = wcfLeaveRecord.LeaveRecord_GetInfo(LeaveRecordID);
                Info2Form();
            }

        }

        private void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        void Info2Form()
        {

            mtxFromDate.Text = _LeaveRecordInfo.FromDate.ToString("dd/MM/yyyy");
            mtxToDate.Text = _LeaveRecordInfo.ToDate.ToString("dd/MM/yyyy");
            txtALDays.Text = _LeaveRecordInfo.ALDays.ToString("#,##0.0");
            txtCODays.Text = _LeaveRecordInfo.CODays.ToString("#,##0.0");
            txtALBefore.Text = _LeaveRecordInfo.ALBefore.ToString("#,##0.0");
            txtCOBefore.Text = _LeaveRecordInfo.COBefore.ToString("#,##0.0");
            isChange = false;
        }

        void Form2Info()
        {
            _LeaveRecordInfo.ID = _LeaveRecordID;
            _LeaveRecordInfo.EmployeeID = _EmployeeID;
            _LeaveRecordInfo.FromDate = UISetting.GetDate(mtxFromDate.Text);
            _LeaveRecordInfo.ToDate = UISetting.GetDate(mtxToDate.Text);
            _LeaveRecordInfo.ALDays = UISetting.GetDecimalNo(txtALDays.Text);
            _LeaveRecordInfo.CODays = UISetting.GetDecimalNo(txtCODays.Text);
            _LeaveRecordInfo.ALBefore = UISetting.GetDecimalNo(txtALBefore.Text);
            _LeaveRecordInfo.COBefore = UISetting.GetDecimalNo(txtCOBefore.Text);

        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (!CheckTextValid.IsDateTime(mtxFromDate.Text, "dd/MM/yyyy"))
            {
                mtxFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDateTime(mtxToDate.Text, "dd/MM/yyyy"))
            {
                mtxToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (DateTime.Compare(UISetting.GetDate(mtxFromDate.Text), UISetting.GetDate(mtxToDate.Text)) > 0)
            {
                mtxToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!CheckTextValid.IsDecimal(txtALDays.Text))
            {
                txtALDays.Focus();
                MessageBox.Show(UpdateUI.GetString("songaynghiphepkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!CheckTextValid.IsDecimal(txtCODays.Text))
            {
                txtCODays.Focus();
                MessageBox.Show(UpdateUI.GetString("songaynghibukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtALBefore.Text))
            {
                txtALBefore.Focus();
                MessageBox.Show(UpdateUI.GetString("songaynghiphepconlaikhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtCOBefore.Text))
            {
                txtCOBefore.Focus();
                MessageBox.Show(UpdateUI.GetString("songaynghibuconglaikhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            LeaveRecordClient wcfLeaveRecord = Service.LeaveRecord();

            Form2Info();
            if (_LeaveRecordInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfLeaveRecord.LeaveRecord_Update(_LeaveRecordInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    wcfLeaveRecord.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _LeaveRecordInfo.ID = wcfLeaveRecord.LeaveRecord_Insert(_LeaveRecordInfo);
                if (_LeaveRecordInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    wcfLeaveRecord.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            _LeaveRecordInfo.ID = 0;
            mtxFromDate.Text = "";
            mtxToDate.Text = "";
            txtALDays.Text = "0.0";
            txtCODays.Text = "0.0";
            txtALBefore.Text = "0.0";
            txtCOBefore.Text = "0.0";
        }

        private void frmLeaveRecord_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void frmLeaveRecord_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void txtALDays_Leave(object sender, EventArgs e)
        {
            txtALDays.Text = UISetting.GetDecimalNo(txtALDays.Text).ToString("#,##0.0");
        }

        private void txtCODays_Leave(object sender, EventArgs e)
        {
            txtCODays.Text = UISetting.GetDecimalNo(txtCODays.Text).ToString("#,##0.0");
        }

        private void txtALBefore_Leave(object sender, EventArgs e)
        {
            txtALBefore.Text = UISetting.GetDecimalNo(txtALBefore.Text).ToString("#,##0.0");

        }
        private void txtCOBefore_Leave(object sender, EventArgs e)
        {

            txtCOBefore.Text = UISetting.GetDecimalNo(txtCOBefore.Text).ToString("#,##0.0");
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
